﻿<%@ WebHandler Language="C#" Class="Manage" %>

using System;
using System.Web;
using System.Collections.Generic;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.Course;
using Company.StudyClub.BLL.Common;
using System.Text;
using System.IO;
using System.Threading;

public class Manage : IHttpHandler {
    
    private HttpHelper helper = new HttpHelper();
    
    public void ProcessRequest (HttpContext context) {
        HttpResponse response = context.Response;

        switch (helper.GetString("type"))
        { 
            case "updatecourse":
                UpdateCourse(response);
                break;                   
            case "uploadcovericon":
                UploadCoverIcon(context);
                break;
            case "deletecovericon":
                DeleteCoverIcon(context);
                break;
            case "deletecourse":
                DeleteCourse(response);
                break;
            default: break;
        }
    }

    private void UpdateCourse(HttpResponse response)
    {
        Course_Info model = new Course_Info();
        model.CourseID = helper.GetInt("courseid");
        model.CourseName = helper.GetString("cname");
        model.PlayLength = helper.GetInt("playlength") * 60;
        model.IsPublic = helper.GetInt("ispublic");
        model.IsRecommend = helper.GetInt("isrecommend");
        model.Keywords = helper.GetString("keywords");
        model.LecturerIntro = helper.GetString("intro");
        model.Description = helper.GetString("description");        
        model.CatalogID = helper.GetInt("catalogid");
        model.CoverIcon = helper.GetString("covericon");
        
        model.Status = -1;
        model.SerialNumber = null;
        model.CreatorID = -1;
        model.CreateTime = DateTime.Parse("1900-1-1");
        model.CourseType = -1;
        model.SchoolID = -1;
        
        CourseBLL objBLL = new CourseBLL();
        objBLL.Update(model);

        int curcatalogid = helper.GetInt("curcatalogid");

        if (curcatalogid != model.CatalogID)
        {
            CatalogBLL objCatalog = new CatalogBLL();
            objCatalog.UpdateTotalCourses(model.CatalogID, 1);
            objCatalog.UpdateTotalCourses(curcatalogid, -1);
        }

        response.Write("1");
    }      

    private void UploadCoverIcon(HttpContext context)
    {
        HttpPostedFile oFile = context.Request.Files["Filedata"];

        string directory = HttpContext.Current.Server.MapPath(@context.Request["folder"]);
        string filename = helper.GetString("covericon");
        
        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);

        string m_filepath = Path.Combine(directory, filename.Replace("_S","_O"));
        oFile.SaveAs(m_filepath);

        CutImage objImage = new CutImage();
        objImage.CutImageBySize(m_filepath, m_filepath.Replace("_O", "_S"), 160, 120);

        int courseId = helper.GetInt("courseid");
        if (courseId > 0)
        {
            CourseBLL objBLL = new CourseBLL();
            objBLL.UpdateCoverInfo(courseId, filename);
        }
        
        context.Response.Write("1");
    }

    private void DeleteCoverIcon(HttpContext context)
    {
        string filepath = context.Server.MapPath("~/data/coursepicture/" + helper.GetString("filename"));

        if (File.Exists(filepath))
        {
            File.Delete(filepath);
        }
        
        string oPicture = filepath.Replace("_S", "_O");
        if (File.Exists(oPicture))
        {
            File.Delete(oPicture);
        }

        int courseId = helper.GetInt("courseid");
        if (courseId > 0)
        {
            CourseBLL objBLL = new CourseBLL();
            objBLL.UpdateCoverInfo(courseId, "");
        }
        context.Response.Write("1");
    }

    private void DeleteCourse(HttpResponse response)
    {
        CourseBLL objBLL = new CourseBLL();

        try
        {
            string[] cids = helper.GetString("cids").Split(',');

            for (int i = 0; i < cids.Length; i++)
            {
                if (cids[i] == string.Empty)
                    continue;


                objBLL.UpdateIsDelete(int.Parse(cids[i]), 1);
            }

            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }
    }

    public bool IsReusable
    {
        get {
            return false;
        }
    }

}